const express = require('express')
const bodyParser = require('body-parser')// body-parser中间件来解析请求体
const dayjs = require('dayjs')

const app = express()

let shipList = [{
    id: 1,
    type: 0,
    name: '测试船只1',
    status: 0,
    createTime: '2022-09-28 17:18:56',
    updateTime: '2022-09-28 17:18:56',
},{
    id: 2,
    type: 1,
    name: '测试船只2',
    status: 1,
    createTime: '2022-09-28 17:18:56',
    updateTime: '2022-09-28 17:18:56',
},{
    id: 3,
    type: 0,
    name: '测试船只3',
    status: 1,
    createTime: '2022-09-28 17:18:56',
    updateTime: '2022-09-28 17:18:56',
}]

// 跨域
app.all('*', function(req, res, next) {
  res.header('Access-Control-Allow-Origin', '*')
  res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With')
  res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS')
  if (req.method === 'OPTIONS') {
    res.sendStatus(200)
  } else {
    next()
  }
})

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

app.set('views', 'dist')
app.set('view engine', 'html')
app.engine('html', require('ejs-mate'))

// 设置路由
app.get('/', function(req, res) {
  res.render('index', { title: 'Hey', message: 'Hello there!' })
})



app.post('/ship/list', function(req, outerRes) {
    console.log('查询了');
    outerRes.send({
        message: '获取成功',
        code: 0,
        data: shipList,
        success: 1
    })
})
app.post('/ship/add', function(req, outerRes) {
    const { name, status, type } = req.body
    const length = shipList.length
    shipList.push({
        id: length + 1,
        name: name,
        createTime: dayjs().format('YY-MM-DD HH:mm:ss'),
        updateTime: dayjs().format('YY-MM-DD HH:mm:ss'),
        status,
        type,
    })
    console.log('新增了');
    outerRes.send({
        message: '新增成功',
        code: 0,
        data: shipList,
        success: 1
    })
})
app.post('/ship/delete', function(req, outerRes) {
    const { ids } = req.body;
    shipList = shipList.filter(item => ids.indexOf(item.id) < 0)
    console.log('删除了');
    outerRes.send({
        message: '删除成功',
        code: 0,
        data: shipList,
        success: 1
    })
})
app.post('/ship/edit', function(req, outerRes) {
    const { id, name, type, status } = req.body;
    for (let index = 0; index < shipList.length; index++) {
        const element = shipList[index];
        if (element.id === id) {
            shipList.splice(index, 1, {
                ...element,
                updateTime:  dayjs().format('YY-MM-DD HH:mm:ss'),
                name,
                type,
                status,
            })
            break;
        }
    }
    console.log('编辑了');
    outerRes.send({
        message: '编辑成功',
        code: 0,
        data: shipList,
        success: 1
    })
})



const server = app.listen(3010, function() {
  const host = server.address().address
  const port = server.address().port
  console.log('Example app listening at ', host, port)
})
